<?php

class Zend_Lvl_Navigation extends Zend_Db_Table_Abstract{
	protected $_name = "category";
	protected $db;
	var $lvl, $_helper_Url, $strMenu = "", $arrSel = array(''=> '---Select product category---'), $arrDis = array(), $arr = array();
	public function __construct(){
		$this->db = Zend_Registry::get('db');
		$this->lvl = new Zend_Lvl();
		$this->_helper_Url = new Zend_View_Helper_Url();
	}
	
	public function getProductsByCid($id){
		$query = $this->db->select()
				->from("products")
				->where("status =?", "Enable")
				->where("cid =?", $id)
				->order("pid ASC");
		$result = $this->db->fetchAll($query);
		for($i=0; $i < count($result); $i++){
			$result[$i]["title"] = $this->lvl->stripUnicode($result[$i]["name"]);
		}
		return $result;
	}
	public function getByPid($id){
		$query = $this->db->select()
				->from("products")
				->where("status =?", "Enable")
				->where("pid =?", $id);
		$result = $this->db->fetchRow($query);
		$result["title"] = $this->lvl->stripUnicode($result["name"]);
	
		return $result;
	}
	public function getByCid($id){
		$query = $this->db->select()
				->from($this->_name)
				->where("status =?", "Enable")
				->where("cid =?", $id);
		$result = $this->db->fetchRow($query);
		$result["title"] = $this->lvl->stripUnicode($result["name"]);

		return $result;
	}
	public function getByParentid($id){
		$query = $this->db->select()
				->from($this->_name)
				->where("status =?", "Enable")
				->where("parent_id =?", $id)
				->order("order1 ASC");
		$result = $this->db->fetchAll($query);
		for($i=0; $i < count($result); $i++){
			$result[$i]["title"] = $this->lvl->stripUnicode($result[$i]["name"]);
		}
		return $result;
	}
	
	public function Navigation(){
		$navParent = $this->getByParentid(0);
		if(count($navParent) > 0){
			$id = 0;
			for ($i = 0; $i < count($navParent); $i++)
			{
				$navChild = $this->getByCid($navParent[$i]["cid"]);
				$id = $navChild["cid"];
				$title_parent = $navParent[$i]["title"];
				$this->Loadnavigation($id, $title_parent);
			}
				
			return $this->strMenu;
		}
	}
		
	public function Loadnavigation($id, $title_parent){
		$navParent = $this->getByCid($id);
		$navChild = $this->getByParentid($id);
		
		if(count($navChild) > 0){
			$this->strMenu .= "\n<li><label>".$navParent["name"]."</label>";
			$this->strMenu .= "\n<ul>";
			for ($i = 0; $i < count($navChild); $i++)
			{
				$this->Loadnavigation($navChild[$i]["cid"],$navParent["title"]);
			}
			$this->strMenu .= "\n</ul>\n</li>";
		}
		else
		{
			$navParent1 = $this->getByPid($id);
			$navChild1 = $this->getProductsByCid($id);
			if(count($navChild1) > 0){
				$parent = $this->lvl->stripUnicode($navParent["name"]);
				if($parent != $title_parent){
					$parent = $title_parent.'-'.$parent;
				}
				$this->strMenu .= "\n<li><label>".$navParent["name"]."</label>";
				$this->strMenu .= "\n<ul>";
				for ($i = 0; $i < count($navChild1); $i++)
				{
					$this->strMenu .= "\n<li><a class=\"sub_menu\" href=\"".$this->_helper_Url->url(array('title_parent' => $parent,'title' => $navChild1[$i]["title"], 'id' => $navChild1[$i]["pid"]), 'subnavigation')."\">".$navChild1[$i]["name"]."</a></li>";
				}
				$this->strMenu .= "\n</ul>\n</li>";
			}
			else
			{
				$this->strMenu .= "\n<li><label>".$navParent["name"]."</label></li>";
				//$this->strMenu .= "\n<li><a class=\"sub_menu\" href=\"".$this->_helper_Url->url(array('title_parent' => $title_parent,'title' => $navParent1["title"], 'id' => $navParent1["pid"]), 'subnavigation')."\">".$navParent1["name"]."</a></li>";
			}
		}
		
		
	}
	
	public function NavigationProduct(){
		$navParent = $this->getByParentid(0);
		if(count($navParent) > 0){
			$id = 0;
			for ($i = 0; $i < count($navParent); $i++)
			{
			$navChild = $this->getByCid($navParent[$i]["cid"]);
			$id = $navChild["cid"];
			$title_parent = $navParent[$i]["title"];
			$this->Loadnavigation2($id);
			}
	
			return $this->strMenu;
		}
	}
	
	public function Loadnavigation2($id){
		$navParent = $this->getByCid($id);
		$navChild = $this->getByParentid($id);
	
		if(count($navChild) > 0){
			$this->strMenu .= "\n<li><label>".$navParent["name"]."</label>";
			$this->strMenu .= "\n<ul>";
			for ($i = 0; $i < count($navChild); $i++)
			{
				$this->Loadnavigation2($navChild[$i]["cid"]);
			}
			$this->strMenu .= "\n</ul>\n</li>";
		}
		else
		{
			$this->strMenu .= "\n<li><a href=\"".$this->_helper_Url->url(array('module' => 'admin','controller' => 'products', 'action' => 'type', 'cid' => $navParent["cid"]), null,true)."\">".$navParent["name"]."</a></li>";
		}
	}
	
	public function Selected(){
		$navParent = $this->getByParentid(0);
		if(count($navParent) > 0){
			$id = 0;
			for ($i = 0; $i < count($navParent); $i++)
			{
				$navChild = $this->getByCid($navParent[$i]["cid"]);
				$id = $navChild["cid"];
				$title_parent = $navParent[$i]["title"];
				$this->Loadselected($id,$navParent[$i]["name"]);
			}
			$this->arr = array($this->arrSel, $this->arrDis);
			return $this->arr;
		}
	}
	
	public function Loadselected($id,$parent_name){
		$navParent = $this->getByCid($id);
		$navChild = $this->getByParentid($id);
	
		if(count($navChild) > 0){
			//$this->arrSel[$navParent["cid"]] = $navParent["name"];
			$this->arrSel[$navParent["name"]] = array();
			array_push($this->arrDis,$navParent["cid"]);
			for ($i = 0; $i < count($navChild); $i++)
			{
				$this->Loadselected($navChild[$i]["cid"],$navParent["name"]);
			}
	
		}
		else
		{
			if (isset($this->arrSel[$parent_name])){
				$this->arrSel[$parent_name][$navParent["cid"]] = $navParent["name"];
			}else{
				$this->arrSel[$navParent["cid"]] = $navParent["name"];
			}
		}
	}
}